import QtQuick 2.1
import QtQuick.Controls 1.0
import QtQuick.Window 2.0
import QtQuick.Dialogs 1.1
import QtQuick.Layouts 1.1


import "runscript.js" as Skript
import "cDataStore.js" as DataStore
import "timer.js" as MyTimer



//---------------------------------------------
ApplicationWindow {

    title: qsTr("Otvor")
    width: 640
    height: 480
    id: mainWindow


    function openFile(){
        fileDialog.open();
        return true;
    }

    function setFileUrl(inURL){
        DataStore.setUrl(inURL);
        txtURL.text = DataStore.getUrl();
        //messageBox.showMessage(DataStore.getUrl(), "Spravicka")

    }

    FileDialog {
        id: fileDialog
        nameFilters: ["Text files (*.txt)", "HTML files (*.html)"]
        //onAccepted: txtURL.text = fileUrl
        onAccepted: setFileUrl(fileUrl)
    }

    Action {
        id: fileOpenAction
        iconSource: "images/fileopen.png"
        iconName: "document-open"
        text: "Open"
        onTriggered: openFile()
    }



    menuBar: MenuBar {
        Menu {
            title: qsTr("File")
            MenuItem {
                text: qsTr("Open")
                action: fileOpenAction
            }

            MenuSeparator {}

            MenuItem {
                text: qsTr("Exit")
                onTriggered: Qt.quit();
            }


        }
    }

    Button {
        x: 8
        y: 8
        text: qsTr("Otvor")
        action: fileOpenAction
    }

    statusBar:StatusBar {
        id: status_bar1
        anchors.right: parent.right
        anchors.left: parent.left
        anchors.bottom: parent.bottom
        Text {
            id: txtURL
            anchors.left: parent.left
            text: DataStore.getUrl()
            onTextChanged: (DataStore.getUrl())

        }
    }


    Window {
        function showMessage(text, title)
        {
            messageBox.text = text;
            messageBox.title = title;
            messageBox.visible = true;
        }
        id: messageBox
        modality: Qt.ApplicationModal
        //title: ""
        visible: false
        property alias text: messageBoxLabel.text
        minimumHeight: 100
        minimumWidth: 300


        y: (mainWindow.height/2-height/2)
        x: (mainWindow.width/2-width/2)

        //flags: Qt.WindowCloseButtonHint


        Label {

            anchors.margins: 10
            anchors.top: parent.top
            anchors.left: parent.left
            anchors.right: parent.right
            anchors.bottom: messageBoxButton.top
            horizontalAlignment: Text.AlignHCenter
            wrapMode: Text.WordWrap
            id: messageBoxLabel
        }

        Button {
            anchors.margins: 10
            id: messageBoxButton
            anchors.bottom: parent.bottom
            anchors.horizontalCenter: parent.horizontalCenter
            text: "Ok"
            onClicked: messageBox.visible = false
        }
    }

    Rectangle {
        function showToast(inText){
            toastLabel.text = inText
            toast.visible = true
            MyTimer.init(3);
            toastTimer.start();
        }

        function tick(){
            if (!MyTimer.tick()){
                toastTimer.stop();
                toast.visible = false;
                toastLabel.text=null;
                //sem doplnit cyklus na znizovanie toast.opacity
            }
        }



        id: toast
        width: 128
        height: 128
        color: "#494949"
        radius: 5
        border.width: 2
        border.color: "#ffffff"
        visible: toastTimer.running
        anchors.centerIn: parent

        MouseArea{
            onClicked: toast.visible = false
        }

        Timer {
            id: toastTimer
            repeat: true
            triggeredOnStart: false
            interval: 1000
            onTriggered: toast.tick()
        }

        Label {
            anchors.centerIn: parent
            wrapMode: Text.WordWrap
            id: toastLabel
            color: "#dddddd"
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignHCenter
        }






    }

    Button {
        id: button1
        x: 99
        y: 8
        text: "Button"
        onClicked: toast.showToast(rgValue)
    }

    ExclusiveGroup { id: group }

    property int rgValue: -2



    Rectangle {
        id: rectangle1
        x: 23
        y: 140
        width: 134
        height: 73
        color: mainWindow.color
        border.width: 1
        border.color: "#adadad"

        RadioButton {
            id: radioButton1
            anchors.left: parent.left
            anchors.top: parent.top
            text: qsTr("Radio Button")
            exclusiveGroup: group
            onClicked: { rgValue = 1 }

        }

        RadioButton {
            id: radioButton2
            anchors.left: parent.left
            anchors.top: radioButton1.bottom
            text: qsTr("Radio Button")
            exclusiveGroup: group
            onClicked: { rgValue = 2 }
        }

        RadioButton {
            id: radioButton3
            anchors.left: parent.left
            anchors.top: radioButton2.bottom
            text: qsTr("Radio Button")
            exclusiveGroup: group
            onClicked: { rgValue = 3 }
    }


    }
    property  bool ckbValue: false
    onCkbValueChanged: {
        lblChecked.visible = ckbValue
    }
    CheckBox {
        id: checkBox1
        x: 23
        y: 230
        text: qsTr("Check Box")
        checked: ckbValue
        onClicked: {ckbValue = this.checked }
    }

    ComboBox {
        id: comboBox1
        x: 23
        y: 271
    }

    Label {
        id: lblChecked
        x: 152
        y: 235
        text: "Checked"
        visible: ckbValue
    }


}


